﻿@model int?
@{
    const string ControllerName = "Participants";
}

@(Html.Kendo().Grid<OJS.Web.Areas.Administration.ViewModels.Participant.ParticipantAdministrationViewModel>()
    .Name("DataGrid")
    .Columns(columns =>
        {
            columns.Bound(col => col.Id);
            columns.Bound(col => col.UserName);
            columns.Bound(col => col.ContestName);
            columns.Bound(col => col.IsOfficial);
            columns.Bound(col => col.CreatedOn).Format("{0:dd/MM/yyyy HH:mm}").Hidden();
            columns.Bound(col => col.ModifiedOn).Format("{0:dd/MM/yyyy HH:mm}").Hidden();
            columns.Command(command => command.Destroy().Text(" ")).Width(80);
        })
    .ToolBar(toolbar =>
    {
        toolbar.Create().Text("Добавяне на участник");
        toolbar.Custom().Text("Обратно към администрацията").Action("Navigation", "Administration", new { Area = "" }).Name("custom-toolbar-button");
        toolbar.Custom().Text("Експорт към Excel").Name("custom-toolbar-button").HtmlAttributes(new { id = "export" }).Url(Url.Action("ExportToExcel", ControllerName, new { page = 1, pageSize = "~", filter = "!!", sort = "~" }));
    })
    .Editable(editable =>
    {
        editable.Mode(GridEditMode.PopUp);
        editable.Window(w => w.Title(ViewBag.Title));
        editable.DisplayDeleteConfirmation("Наистина ли искате да изтриете елемента?");
    })
    .ColumnMenu()
    .ClientDetailTemplateId("questions-answers-template")
    .Events(e => e.DataBound("onDataBound").Edit("onEdit"))
    .Pageable(x => x.Refresh(true))
    .Sortable(x => x.Enabled(true).AllowUnsort(false))
    .Filterable(x => x.Enabled(true))
        /*.Groupable(x =>
        {
            x.Enabled(true);
            x.Messages(m => m.Empty("Хванете заглавието на колона и го преместете тук, за да групирате по тази колона."));
        })*/
    .Reorderable(x => x.Columns(true))
    .Resizable(x => x.Columns(true))
    .DataSource(datasource => datasource
        .Ajax()
        .ServerOperation(true)
        .Model(model =>
            {
                model.Id(m => m.Id);
            })
        .Sort(sort => sort.Add(x => x.CreatedOn).Descending())
        .Create(create => create.Action("Create", ControllerName))
        .Read(read => read.Action("ReadParticipants", ControllerName, new { id = Model }))
        .Destroy(destroy => destroy.Action("Destroy", ControllerName))
        .Events(ev => ev.Error("validateModelStateErrors"))
    )
)

<script type="text/javascript">
    function onDataBound(e) {
        CreateExportToExcelButton();
    }

    function onEdit(e) {
        var contestComboBox = e.container.find('#ContestId').data('kendoComboBox');
        contestComboBox.value(null);
        contestComboBox.text(null);
    }
</script>

<script type="text/x-kendo-template" id="questions-answers-template">
    @(Html.Kendo().Grid<OJS.Web.Areas.Administration.ViewModels.Participant.ParticipantAnswerViewModel>()
        .Name("DetailQuestionsAnswersGrid_#=Id#")
        .Columns(columns =>
        {
            columns.Bound(m => m.QuestionText);
            columns.Bound(m => m.Answer);
            columns.Command(com => com.Edit().Text(" ")).Width(80);
        })
        .ColumnMenu()
        .Editable(edit =>
            {
                edit.Mode(GridEditMode.PopUp);
                edit.Window(w => w.Title(ViewBag.Title));
                edit.DisplayDeleteConfirmation("Наистина ли искате да изтриете елемента?");
            })
        .DataSource(data =>
        {
            data.Ajax()
                .ServerOperation(true)
                .Model(model =>
                {
                    model.Id(m => m.ContestQuestionId);
                })
                .Sort(sort => sort.Add(field => field.ContestQuestionId))
                .Read(read => read.Action("Answers", ControllerName, new { id = "#= Id #" }))
                .Update(update => update.Action("UpdateParticipantAnswer", ControllerName));
        })
        .ToClientTemplate())
</script>

@if (Model.HasValue)
{
    <script>
        var exportButton = $('#export');
        var hrefAttr = exportButton.attr('href');
        var linkParts = hrefAttr.split('?');
        var url = linkParts[0] + 'ByContest?' + linkParts[1] + "&contestId=@Model.Value";
        exportButton.attr('href', url);
    </script>
}
